package org.mysfit.ctp4j.infra.pojo;

import java.util.Calendar;
import java.util.List;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

/**
 * 
 * @author long.gf
 *
 */
@Component
@ConfigurationProperties(prefix = "ctp")
public class CtpConfig {

	private String tradeFront;
    private String tradeFront1;
    private String tradeFront2;
    private String marketFront;
    private String marketFront1;
    private String marketFront2;
    private List<CtpUser> users;
    
	public String getTradeFront1() {
		return tradeFront1;
	}
	public void setTradeFront1(String tradeFront1) {
		this.tradeFront1 = tradeFront1;
	}
	public String getTradeFront2() {
		return tradeFront2;
	}
	public void setTradeFront2(String tradeFront2) {
		this.tradeFront2 = tradeFront2;
	}
	public String getMarketFront1() {
		return marketFront1;
	}
	public void setMarketFront1(String marketFront1) {
		this.marketFront1 = marketFront1;
	}
	public String getMarketFront2() {
		return marketFront2;
	}
	public void setMarketFront2(String marketFront2) {
		this.marketFront2 = marketFront2;
	}
	public List<CtpUser> getUsers() {
		return users;
	}
	public void setUsers(List<CtpUser> users) {
		this.users = users;
	}
	public void setTradeFront(String tradeFront) {
		this.tradeFront = tradeFront;
	}
	public void setMarketFront(String marketFront) {
		this.marketFront = marketFront;
	}
	
	public String getTradeFront() {
		if(StringUtils.isEmpty(this.tradeFront)) {
			if(isTradeTime()) {
				return this.tradeFront1;
			}
			return this.tradeFront2;
		}else {
			return this.tradeFront;
		}
	}
	
	public String getMarketFront() {
		if(StringUtils.isEmpty(this.marketFront)) {
			if(isTradeTime()) {
				return this.marketFront1;
			}
			return this.marketFront2;
		}else {
			return this.marketFront;
		}
	}
	
	// 交易日 9:00 ~ 16:00用第一套，其他时间用第二套
	private boolean isTradeTime() {
		Calendar calendar = Calendar.getInstance();
		int day = calendar.get(Calendar.DAY_OF_WEEK);
		if(day > 1 && day < 7) {
			int hour = calendar.get(Calendar.HOUR_OF_DAY);
			if(hour >= 9 && hour <= 15) {
				return true;
			}
		}
		return false;
	}
}
